import 'package:dss_base/dss_base.dart';
import 'package:dss_base/ext/string_ext.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo_test/pages/gift_page.dart';
import 'package:flutter_demo_test/pages/home_page.dart';
import 'package:flutter_demo_test/main_viewmodel.dart';
import 'package:flutter_demo_test/pages/message_page.dart';
import 'package:flutter_demo_test/pages/my_page.dart';
import 'package:flutter_demo_test/pages/network/net_a_page.dart';
import 'package:flutter_demo_test/pages/network/net_b_page.dart';
import 'package:flutter_demo_test/pages/network/net_c_page.dart';
import 'package:flutter_demo_test/pages/shop_page.dart';
import 'package:flutter_demo_test/pages/test/test_page.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<StatefulWidget> createState() => _MainPageState();
}

class _MainPageState extends BaseState<MainPage, MainViewModel> {
  @override
  MainViewModel onCreateViewModel() => MainViewModel();

  int _currentIndex = 0;

  final _items = <BottomNavigationBarItem>[];
  final _pages = <Widget>[];

  @override
  void initState() {
    super.initState();
    _items.add(
      _buildItem(
        label: 'Home',
        normalPath: 'main_nav/ic_home_normal.png',
        selectedPath: 'main_nav/ic_home_selected.png',
      ),
    );
    _pages.add(HomePage());

    _items.add(
      _buildItem(
        label: 'Message',
        normalPath: 'main_nav/ic_message_normal.png',
        selectedPath: 'main_nav/ic_message_selected.png',
      ),
    );
    _pages.add(MessagePage());

    _items.add(
      _buildItem(
        label: 'Online shop',
        normalPath: 'main_nav/ic_shop_normal.png',
        selectedPath: 'main_nav/ic_shop_selected.png',
      ),
    );
    _pages.add(ShopPage());

    _items.add(
      _buildItem(
        label: 'Gift Coin',
        normalPath: 'main_nav/ic_gift_normal.png',
        selectedPath: 'main_nav/ic_gift_selected.png',
      ),
    );
    _pages.add(GiftPage());

    _items.add(
      _buildItem(
        label: 'Account',
        normalPath: 'main_nav/ic_my_normal.png',
        selectedPath: 'main_nav/ic_my_selected.png',
      ),
    );
    _pages.add(MyPage());
  }

  @override
  Widget onBuild(BuildContext context) {
    return Scaffold(
      body: IndexedStack(index: _currentIndex, children: _pages),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        backgroundColor: Colors.white,
        type: BottomNavigationBarType.fixed,
        unselectedFontSize: 10.sp,
        selectedFontSize: 10.sp,
        unselectedItemColor: ColorsX.mainText,
        selectedItemColor: ColorsX.redText,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: _items,
      ),
    );
  }

  /// 底部导航 tab item
  BottomNavigationBarItem _buildItem({
    required String label,
    required String normalPath,
    required String selectedPath,
    double normalSize = 20,
    double selectedSize = 20,
    int num = 0,
  }) {
    return BottomNavigationBarItem(
      icon: _buildIcon(normalPath, normalSize.w, num),
      activeIcon: _buildIcon(selectedPath, selectedSize.w, num),
      label: label,
    );
  }

  Widget _buildIcon(String path, double size, int num) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.only(bottom: 2.w),
      child: Stack(
        children: [
          Center(child: Image.asset(path.toAssetsImagesPath(), width: size)),
          if (num > 0)
            Positioned(
              right: 15,
              top: 0,
              child: BadgeWidget(num: num),
            ),
        ],
      ),
    );
  }
}
